package com.hys.authserver.utils;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * IDE: IntelliJ IDEA
 * 项目: auth-server
 * 日期: 2024/9/18 23:50:31
 * 作者: qscy
 * 功能:
 **/
@Component
public class RedisUtils {

    public static final String SMS_CODE_KEY="auth:sms_code:login:";
    public static final String JWT_UUID_KEY="auth:jwt_uuid:";

    public static final long JWT_TOKEN_EXPIRE_TIME=60*60*24*7;


    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 发短信验证码
     * @param phone
     * @param code
     */
    public void setSmsCodeKey(String phone,String code){
        stringRedisTemplate.opsForValue().set(SMS_CODE_KEY+phone,code,60*5, TimeUnit.SECONDS);
    }

    public String getSmsCodeKey(String phone){
        return stringRedisTemplate.opsForValue().get(SMS_CODE_KEY+phone);
    }

    public void deleteSmsCodeKey(String phone){
        stringRedisTemplate.delete(SMS_CODE_KEY+phone);
    }
    /**
     * 设置jwt有效性,首先判断条件
     *  进行续期
     */
    public void setJwtUuidKey(String username,String uuid){
        stringRedisTemplate.opsForValue().set(RedisUtils.JWT_UUID_KEY+username, uuid, RedisUtils.JWT_TOKEN_EXPIRE_TIME, TimeUnit.SECONDS);
    }

    public String getJwtUuidKey(String username){
        return stringRedisTemplate.opsForValue().get(RedisUtils.JWT_UUID_KEY+username);
    }

    public void deleteJwtUuidKey(String username){
        stringRedisTemplate.delete(RedisUtils.JWT_UUID_KEY+username);
    }

}
